package dao

import (
	"encoding/json"
	"server_cluster/http/model"
)

// GetGmServerConfig 获取服务器配置
func (d *Dao) GetGmServerConfig() (*model.DbGmServerConfig, error) {
	gmServerConfig := new(model.DbGmServerConfig)
	err := d.db.Where("id = ?", 1).First(&gmServerConfig).Error
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(gmServerConfig.IpWhiteListJson), &gmServerConfig.IpWhiteList)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(gmServerConfig.UidWhiteListJson), &gmServerConfig.UidWhiteList)
	if err != nil {
		return nil, err
	}
	return gmServerConfig, nil
}

// SetGmServerConfig 更新服务器配置
func (d *Dao) SetGmServerConfig(gmServerConfig *model.DbGmServerConfig) error {
	gmServerConfig.Id = 1
	ipWhiteList, err := json.Marshal(gmServerConfig.IpWhiteList)
	if err != nil {
		return err
	}
	gmServerConfig.IpWhiteListJson = string(ipWhiteList)
	uidWhiteList, err := json.Marshal(gmServerConfig.UidWhiteList)
	if err != nil {
		return err
	}
	gmServerConfig.UidWhiteListJson = string(uidWhiteList)
	err = d.db.Save(gmServerConfig).Error
	if err != nil {
		return err
	}
	return nil
}
